package com.macro.mall.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsFunParam;
import com.macro.mall.mapper.BmsFunMapper;
import com.macro.mall.model.BmsFun;
import com.macro.mall.model.BmsFunExample;
import com.macro.mall.service.BmsFunService;
import com.macro.mall.utils.UserUtils;
import cn.hutool.core.date.DateTime;

@Service
public class BmsFunServiceImpl implements BmsFunService {
    @Autowired
    private BmsFunMapper bmsFunMapper;
    
    @Autowired
    private UserUtils userUtils;

    /**
     * 添加浙东趣事
     */
    @Override
    @Transactional
    public int create(BmsFunParam bmsFunParam) {
        BmsFun bmsFun = new BmsFun();
        BeanUtils.copyProperties(bmsFunParam, bmsFun);
        Long userId = userUtils.getUserId();
        bmsFun.setCreateUser(userId);
        bmsFun.setUpdateUser(userId);
        bmsFun.setCreateTime(new DateTime());
        bmsFun.setUpdateTime(new DateTime());
        int count = bmsFunMapper.insertSelective(bmsFun);
        return count;
    }

    /**
     * 分页查询
     */
    @Override
    public List<BmsFun> getList(BmsFunParam bmsFunParam) {
        PageHelper.startPage(bmsFunParam.getPageNum(), bmsFunParam.getPageSize());
        BmsFunExample example = new BmsFunExample();
        BmsFunExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteNotEqualTo(true);
        criteria.andTitleLike("%" + bmsFunParam.getTitle() + "%");
        List<BmsFun> list = bmsFunMapper.selectByExample(example);
        return list;
    }

    /**
     * 编辑浙东趣事
     */
    @Override
    @Transactional
    public int update(BmsFunParam bmsFunParam) {
        BmsFun express = new BmsFun();
        express.setCoverPic(bmsFunParam.getCoverPic());
        express.setTitle(bmsFunParam.getTitle());
        express.setStatus(bmsFunParam.getStatus());
        express.setRemark(bmsFunParam.getRemark());
        express.setDescription(bmsFunParam.getDescription());
        Long userId = userUtils.getUserId();
        express.setUpdateUser(userId);
        express.setUpdateTime(new DateTime());
        BmsFunExample example = new BmsFunExample();
        example.createCriteria().andIdEqualTo(bmsFunParam.getId());
        int count = bmsFunMapper.updateByExampleSelective(express, example);
        return count;
    }
    /**
     * 根据id查询
     */
    @Override
    public BmsFun getItem(Long id) {
        return bmsFunMapper.selectByPrimaryKey(id);
    }

    /**
     * 删除浙东趣事
     */
    @Override
    @Transactional
    public int delete(List<Long> ids) {
        BmsFun express = new BmsFun();
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        express.setIsDelete(true);
        express.setUpdateUser(userId);
        express.setUpdateTime(new DateTime());
        
        BmsFunExample example = new BmsFunExample();
        example.createCriteria().andIdIn(ids);
        int count = bmsFunMapper.updateByExampleSelective(express, example);
        return count;
    }

}
